Helpful Information
 
 
Category: PHP Export
PHP Export > csv (I have looked)

Hello :)

I have searched the forums looking for some help on what I am trying to achieve. I'll explain what I am trying to do.

I have got a page on a site that I'm working on. As you can see there are 3 drop down menu's which are dynamicaly driven by 3 tables within my database. On selection of the lists, you are taken to the same page (show if statement - dreamweaver) which shows you the record count. This is based on your criteria!
At this point. I would like to export the query results into a .csv file. Now I have looked across the forums and found a few things and tried to implement them into my site, all of which are bringing up an empty page. Possibly because my search queries are dynamic?

- Code has been taken out due to amount of rendering - fr0sty

If you wish to see the code, ask please. Can anyone help me?

fr0sty

I don't have the time or inclination to sit here and read through all the code you've posted. Can you post just the code that you've written to export to csv so we can see the problem you're having?

Ewww you can tell that was Dreamweaver! Um ok, first of all check which version of PHP you have installed, if it is anything =>4.1 change your $HTTP_**_VARS to the new standard $_**, so where you are using $HTTP_SERVER_VARS this is now $_SERVER instead.

I assume you are not getting any errors from the or die statements no?

Originally posted by micros_bytes
I don't have the time or inclination to sit here and read through all the code you've posted. Can you post just the code that you've written to export to csv so we can see the problem you're having?
Thats the bit Im trying to do... Trying to write code to actually export my search.

- fr0styxm

Originally posted by SilkySmooth
Ewww you can tell that was Dreamweaver! Um ok, first of all check which version of PHP you have installed, if it is anything =>4.1 change your $HTTP_**_VARS to the new standard $_**, so where you are using $HTTP_SERVER_VARS this is now $_SERVER instead.

I assume you are not getting any errors from the or die statements no?

Hi,
Thank you for your reply!. Firstly I am not getting any errors or die statements. the code pasted works, its the code to actually do the results. What I want is to implement into that some code to export my result onto the screen as the count (rSCount) and also then into a .csv file which is automatically downloaded.

I am using php 4.2.3 - mysql 3.23.56.

Cheers

Funnily enough, I wrote code to export to CSV just yesterday. This is it: (Note: You'll need to make a lot of adjustments to it to get it to work for you)


#
#
# Thursday - 3rd July 2003
# Title: Export to CSV
# Author:
# Purpose: Export recordsets to CSV
#
#

if($subscribers->RecordCount() <= 0)
{
// No records
print 'No Records';
}
else if (file_exists($filename)) {
print 'File Already Exists, must give unique name.';
}
else
{
$content = "name,company,department,email,address1,address2,address3,telephone,date,added by\r\n";
while(!$subscribers->EOF)
{
# Format the content to be written to file
$content.=$name.",";
$content.=$company.",";
$content.=$department.",";
$content.=$email.",";
$content.=$address1.",";
$content.=$address2.",";
$content.=$address3.",";
$content.=$telephone.",";
$content.=$theDate.",";
$content.=$addedby."\r\n";

# Move to the next record
$subscribers->MoveNext();
}

# Create the file
if (touch ($filename)) {
}
else
{
print 'File could not be created!';
break;
}

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

if (!$handle = fopen($filename, 'a')) {
print "Cannot open file ($filename)";
exit;
}

if (!fwrite($handle, $content)) {
print "Cannot write to file ($filename)";
exit;
}

print "Your file is <a href=\"$filename\">here</a><p />";
fclose($handle);

} else {
print "The file $filename is not writable";
}
}
}

OK. Exporting the results of a query to csv is really simple.

This example uses PEAR for the database stuff but you can apply the same to whatever you're using.



// database connection left out because you know how to do that
$fh = fopen("/path/to/somefile.csv","w");
$query = "select * from sometable";
$rs = $dbh->query($query);
while($row = $rs->fetchRow()){
$line = join(",",$row);
$line = "$line\n";
fputs($fh,$line);
}
fclose($fh);

Originally posted by micros_bytes
OK. Exporting the results of a query to csv is really simple.

This example uses PEAR for the database stuff but you can apply the same to whatever you're using.



// database connection left out because you know how to do that
$fh = fopen("/path/to/somefile.csv","w");
$query = "select * from sometable";
$rs = $dbh->query($query);
while($row = $rs->fetchRow()){
$line = join(",",$row);
$line = "$line\n";
fputs($fh,$line);
}
fclose($fh);


Hi Microbytes,

I have tried your code.. I did change it slightly cos I have already got my query designed. If you look at my code below.



$fh = fopen("test/somefile.csv","w");
$rs = $dbh->query($query_rSCount);
while($row = $rs->fetchRow()){
$line = join(",",$row);
$line = "$line\n";
fputs($fh,$line);
}
fclose($fh);


The file gets created, by webd user.. but there is nothing in the file. I have taked out your line
$query = "select * from sometable"; As the query has already been created. When i load the page after doing the search, it goes blank and cannot find anything in my error files. Any ideas? The folder test/ does have 777 access so it will work

Try adding some error handling to the script to see if that points to a problem.




$fh = fopen("test/somefile.csv","w");
$rs = $dbh->query($query_rSCount);
if(DB::isError($rs)){
die("Error retrieving data<br>SQL: $query".$rs->getMessage();
}
while($row = $rs->fetchRow()){
$line = join(",",$row);
$line = "$line\n";
fputs($fh,$line) or die("Error writing line to file<br>DATA: $line");
}
fclose($fh);

Hi Microbytes,

Nothing at all... The screen just goes white.

I've tried the putting the code in where all the MySQL statements are above <head> I've tried putting the code in <?php ?> towards the bottom of the page to have it render the other stuff first. Nothing.

If you want to see the page etc, let me know I'll msg you the details.

Thank you for your help so far!

fr0sty

fr0styxm,

Send me the code and I'll talk a look. It's gotta be something pretty simple that's breaking it.

I have posted to you in a prvt msg :)

thank you










privacy (GDPR)